<template>
	<om-addpage :menuName="menuName" :busCode="busCode" @formSubmit="formSubmit">
		<view v-show="false">
			<input v-model="formdata.zposId" name="zposId" />
			<input v-model="formdata.zposName" name="zposName" />
			<input v-model="formdata.adjustCode" name="adjustCode" />
			<input v-model="formdata.adjustName" name="adjustName" />
			<input v-model="formdata.otherAmounts" name="otherAmounts" />
			<input v-model="formdata.userId" name="userId" />
			<input v-model="formdata.remark" name="remark" />
		</view>
		<uni-list title="公积金信息">
			<uni-list-item :showArrow="false">
				<view class="tui-flex">
					<view class="tui-col-4"><om-label label="制表岗位" required></om-label></view>
					<view class="tui-right tui-col-8">
						<picker mode="selector" name="zposIndex" @change="zposFunc" :range="zposList" :value="zposIndex" range-key="posname">
							<view>{{ zposList.length && ~zposIndex ? zposList[zposIndex].posname : '请选择' }}</view>
						</picker>
					</view>
				</view>
			</uni-list-item>
			<uni-list-item  :showArrow="false">
				<view class="tui-flex">
					<view class="tui-col-4"><om-label label="姓名"></om-label></view>
					<view class="tui-right tui-col-8">{{ userName }}</view>
				</view>
			</uni-list-item>
			<uni-list-item  :showArrow="false">
				<view class="tui-flex">
					<view class="tui-col-4"><om-label label="入职日期"></om-label></view>
					<view class="tui-right  tui-col-8">{{ date }}</view>
				</view>
			</uni-list-item>
			<uni-list-item  :showArrow="false">
				<view class="tui-flex">
					<view class="tui-col-4"><om-label label="计酬岗位"></om-label></view>
					<view class="tui-right  tui-col-8">{{ salaryPosName }}</view>
				</view>
			</uni-list-item>
			<uni-list-item  :showArrow="false">
				<view class="tui-flex">
					<view class="tui-col-4"><om-label label="所属机构"></om-label></view>
					<view class="tui-right  tui-col-8">{{ instName }}</view>
				</view>
			</uni-list-item>
			<uni-list-item  :showArrow="false">
				<view class="tui-flex">
					<view class="tui-col-4"><om-label label="调整类别" required></om-label></view>
					<view class="tui-right tui-col-8">
						<picker mode="selector" name="adjustIndex" @change="adjustFunc" :range="adjustList" :value="adjustIndex" range-key="dicname">
							<view>{{ adjustList.length && ~adjustIndex ? adjustList[adjustIndex].dicname : '请选择' }}</view>
						</picker>
					</view>
				</view>
			</uni-list-item>
			<uni-list-item :custom="true" v-if="!ishidden" :showArrow="false">
					<uni-combox
						label="缴费金额"
						labelWidth="33.33333333%"
						required
						:candidates="levelList" 
						placeholder="请选择或输入金额" 
						v-model="formdata.otherAmounts">
					</uni-combox>
			</uni-list-item>
			<uni-list-item  :showArrow="false">
				<view class="tui-flex">
					<view class="tui-col-4"><om-label label="说明" required></om-label></view>
					<view class="tui-left tui-col-8"><textarea :auto-height="true" class="om-textarea" v-model="formdata.remark" name="remark" placeholder="请输入..." /></view>
				</view>
			</uni-list-item>
		</uni-list>
	</om-addpage>
</template>

<script>
import dateTimePicker from '@/static/js/dateTimePicker.js';
import {loadZpos} from '../../../static/js/formOld.js';
import { ignoreHtml } from '@/static/js/common.js';
import { mapState } from 'pinia';
import {useLoginUserStore} from '@/stores/login-user.js';
export default {
	provide() {
		return {
			formPage: this
		};
	},
	data() {
		return {
			formdata: {
				zposId: '',
				zposName: '',
				adjustCode: '',
				adjustName: '',
				otherAmounts: '',
				userId: '',
				remark: ''
			},
			zposIndex: 0,
			zposList: [],
			date: '',
			salaryPosName: '',
			instName: '',
			adjustIndex: 0,
			adjustList: [],
			DICT_ADJUST_TYPE6_3: '',
			dict_level: '',
			levelList: [],
			ishidden: true,
			otherAmounts: '',
			menuName: '',
			id: '',
			busCode: ''
		};
	},
	computed: mapState(useLoginUserStore, ['userName', 'userId']),
	methods: {
		/**
		 * 制表岗位
		 */
		zposFunc(e) {
			let _that = this;
			_that.zposIndex = e.target.value;
			_that.afterZposFunc();
		},
		/**
		 * 加载调整类别
		 */
		loadAdjustFunc() {
			let _that = this;
			_that.om
				.request('wxHousingFund/getAdjustList', {
					userId: _that.formdata.userId
				})
				.then(res => {
					//if(res instanceof Array){
					if(Object.prototype.toString.call(res)==='[object Array]'){
						_that.adjustList = res;
						this.setAdjustValue();
					}
				});
		},
		adjustFunc(e) {
			this.adjustIndex = e.target.value;
			this.setAdjustValue();
		},
		setAdjustValue() {
			if(~this.adjustIndex){
				let code = this.adjustList[this.adjustIndex].code;
				if (code == this.DICT_ADJUST_TYPE6_3) {
					this.ishidden = true;
				} else {
					this.ishidden = false;
				}
				this.formdata.adjustCode = code;
				this.formdata.adjustName = this.adjustList[this.adjustIndex].dicname;
			}
		},
		/**
		 * 历史记录
		 */
		recordFunc(e) {
			this.om.openHistoryPage(this.menuName, this.busCode);
		},

		formSubmit: function(formdata) {
			let _that = this;
			if(!formdata || Object.keys(formdata).length==0){
				formdata = _that.formdata
			}
			if (!formdata.zposId || !formdata.adjustCode|| !formdata.remark) {
				_that.om.toast('必输项未输入完成，请继续编辑');
				return;
			}
			if (formdata.otherAmounts && !/(^[1-9]\d*$)/.test(formdata.otherAmounts)) {
				_that.om.toast('缴费金额请输入正整数');
				return;
			}
			if (formdata.adjustCode != this.DICT_ADJUST_TYPE6_3 && !formdata.otherAmounts) {
				_that.om.toast('缴费金额未输入');
				return;
			}
			_that.om.formSave('wxHousingFund/save', formdata, _that);
		},
		initLoad() {
			let _that = this;
			loadZpos(_that, 'afterZposFunc','您在OM系统中没有任职岗位，不能申请公积金！');

			//加载缴费档次类别
			_that.om
				.request('tsysdict/findChildrenByCodeNoIsdel', {
					code: _that.dict_level
				})
				.then(res => {
					//if (res instanceof Array) {
					if(Object.prototype.toString.call(res)==='[object Array]'){
						_that.levelList = res.map(e=>e.referNum.toString());
					}
				});
		},
		/**
		 * 选择岗位后
		 */
		afterZposFunc() {
			let _that = this;
			_that.om
				.request('wxHousingFund/afterZpos', {
					zposId: _that.zposList[_that.zposIndex].id
				})
				.then(res => {
					if (res.succeed) {
						let data = res.value;
						if (data.tip) {
							_that.om.toast(ignoreHtml(data.tip));
							_that.zposIndex = -1;
							return;
						}
						if (data.user) {
							_that.date = data.user.entryDate;
							_that.salaryPosName = data.user.salaryPosName;
							_that.instName = data.user.instName;
							//加载调整类别
							_that.loadAdjustFunc();
						}
					}
				});
		}
	},
	onLoad: function(options) {
		let _that = this;
		_that.menuName = options.title || '公积金';
		uni.setNavigationBarTitle({
			title: _that.menuName
		});
		_that.busCode = options.code;
		_that.formdata.userId = this.userId
		_that.om.request('wxHousingFund/getConst').then(res => {
			if (res.succeed) {
				_that.busCode = res.value.busCode || 'HRS1302';
				_that.DICT_ADJUST_TYPE6_3 = res.value.DICT_ADJUST_TYPE6_3; //退保
				_that.dict_level = res.value.dict_level;
				_that.initLoad();
			}
		});
	}
};
</script>

<style></style>
